package com.example.aevovein.data;

import android.content.Context;
import android.widget.Toast;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class DataInitializer {
    private static final String TAG = "DataInitializer";
    private final Context context;
    private final Gson gson;
    private final SkilltreeInitializer skilltreeInitializer;

    public DataInitializer(Context context) {
        this.context = context;
        this.gson = new GsonBuilder().setPrettyPrinting().create();
        this.skilltreeInitializer = new SkilltreeInitializer();
    }

    public void initializeData() {
        try {
            // 创建数据目录
            File dataDir = new File(Environment.getExternalStorageDirectory(), "AevoVein/data");
            if (!dataDir.exists()) {
                if (!dataDir.mkdirs()) {
                    Log.e(TAG, "Failed to create data directory");
                    Toast.makeText(context, "创建数据目录失败", Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            // 初始化技能树数据
            initSkilltreeFile(dataDir);
            
            Log.d(TAG, "Data initialization completed successfully");
            Toast.makeText(context, "数据初始化完成", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, "Error during data initialization: " + e.getMessage(), e);
            Toast.makeText(context, "数据初始化失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    // 初始化技能数据文件
    public void initSkilltreeFile(File dataDir) {
        skilltreeInitializer.initialize(context, dataDir);
    }
}